import time
import datetime
import math


# 期数 计算类，得到当前时间的期数
# 10:00-22:00，共72期, 22点后5分钟1期，22:00-01:55，共48期
# 当天凌晨第1期-1:55 一共23期，5分钟一期，22-24 5分钟一期
class Lottery:
    # current = None
    time_subsection = {
        "1": {'start_time': "10:00:00", "end_time": "21:59:59"},
        "2": {'start_time': "22:00:00", "end_time": "23:59:59"},
        "3": {'start_time': "00:00:00", "end_time": "01:59:59"}
    }
    interval = {"1": 10, "2": 5, "3": 5}
    period_subsection = {"1": {"min": 24, "max": 95}, "2": {"min": 96, "max": 120}, "3": {"min": 0, "max": 23}}
    current_period = None
    period_prefix = None

    def __init__(self):
        self.current()

    # 期数前缀
    def getPeriodPrefix(self, day=0):
        time_format = "%y%m%d"
        # 当天
        if day == 0:
            # 格式化时间
            self.period_prefix = time.strftime(time_format)
        elif day == -1:
            timestamp = time.time();
            today_timestamp = timestamp - (timestamp % 86400)
            yestoday_timestamp = today_timestamp-86400
            localtime = time.localtime(yestoday_timestamp)
            self.period_prefix = time.strftime(time_format,localtime)
        elif day == 1:
            timestamp = time.time();
            today_timestamp = timestamp - (timestamp % 86400)
            yestoday_timestamp = today_timestamp+86400
            localtime = time.localtime(yestoday_timestamp)
            self.period_prefix = time.strftime(time_format,localtime)

        return self.period_prefix

    # 当前期
    def current(self):
        current_time = int(time.time())

        for key, times in self.time_subsection.items():
            start_time = self.toTimestamp(times['start_time'])
            end_time = self.toTimestamp(times['end_time'])

            if start_time <= current_time <= end_time:
                run_time = current_time - start_time
                interval_time = self.interval[key]
                run_period = math.ceil((run_time / 60) / interval_time)
                start_period = self.period_subsection[key]['min']
                self.current_period = start_period + run_period

        # 如果当前时间不在时间段内，则初始化当前期数失败，给予期数默认值
        if self.current_period is None:
            self.current_period = 23

        self.current_period = str(self.current_period).zfill(3)
        return "{prefix}-{current_period}".format(prefix=self.getPeriodPrefix(), current_period=self.current_period)

    # 格式化时间戳
    def toTimestamp(self, hour_time_format):
        time_format = "%Y-%m-%d {time}".format(time=hour_time_format)
        # 格式化时间
        datetime_format = time.strftime(time_format)
        # 根据时间格式 创建struct_time
        time_struct = time.strptime(datetime_format, "%Y-%m-%d %H:%M:%S")
        timestamp = int(time.mktime(time_struct))
        return timestamp

    # 下一期
    def next(self):
        day = 0
        next_period = int(self.current_period) + 1
        if next_period > 120:
            next_period = 1
            day = 1

        next_period = str(next_period).zfill(3)
        return "{prefix}-{current_period}".format(prefix=self.getPeriodPrefix(day), current_period=next_period)

    # 上一期
    def pre(self):
        day = 0
        pre_period = int(self.current_period) - 1
        if pre_period <= 0:
            pre_period = 120
            day = -1

        pre_period = str(pre_period).zfill(3)
        return "{prefix}-{current_period}".format(prefix=self.getPeriodPrefix(day), current_period=pre_period)

    def getPeriod(self):
        return self.current_period


if __name__ == "__main__":
    lo = Lottery()
    print(lo.getPeriodPrefix())
    print(lo.current())
    print(lo.pre())
    print(lo.next())
